/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * Create a new topic windows
 */
qx.Class.define("bip.ui.users.WinNewAccount", 
{
	extend: bip.ui.win.Dialog,

	type: "singleton",
	
	/*
	*****************************************************************************
	 EVENTS
	*****************************************************************************
	*/  
	events :
	{
    /** Fired when a new account has been created successfully */
    "newAccount" : "qx.event.type.Data"
		
	},

	/*
	*****************************************************************************
	 PROPERTIES
	*****************************************************************************
	*/ 
	properties: {
		
  	},

	/*
	*****************************************************************************
	 CONSTRUCTOR
	*****************************************************************************
	*/
	
	/**
	 * Construct the window
	 */	
 	construct: function()
	{
		this.base(arguments,this.tr("Create a new account"),this.tr("Create a new account"),null,"icon/48/apps/preferences-users.png");
		this.set({
			width: 250,
			height: 300
		});
		
		this.__service = new bip.services.UserService();
		this.__service.addListener("loaded",function(evt) {
			this.setStateWithDescription("ok",this.tr("User with id \'%1\' has been created",evt.getData().getIdUser()));
			this.debug(this.__model.getName(),this.__model.getPassword());
			this.fireDataEvent("newAccount",{userid:this.__model.getName(),password:this.__model.getPassword()});
		},this);
		this.__service.addListener("error",function(evt) {
			this.setStateWithDescription("error",evt.getData().getMessage());
		},this);
	},

	/*
	*****************************************************************************
	 MEMBERS
	*****************************************************************************
	*/	
	members:
	{	
		__model: null,
		__form: null,
		__service: null,
		
		/**
     * Create the content of the dialog. 
     * Extending classes must implement this method.
     */
    _createWidgetContent : function()
    {
    	this.__createActionBar(this.getChildControl("actionbar"));
    	this.setLayout(new qx.ui.layout.VBox(5));
    	// create form
    	this.__form = new qx.ui.form.Form();
    	this.__form.add(new qx.ui.form.TextField().set({width:150, required:true}),"Name",null,"name");
    	this.__form.add(new qx.ui.form.PasswordField().set({width:150, required:true}),"Password",null,"password");
    	var formController = new qx.data.controller.Form(null, this.__form,false);
  		this.__model = formController.createModel();
		  this.add(new qx.ui.form.renderer.Single(this.__form),{flex:1});
    },
    
    /**
		 * Fill up the controler with the button bar
		 */
		__createActionBar: function(control) {
			var saveCmd = new qx.ui.core.Command();
			saveCmd.addListener("execute",this.__onSaveButtonClickHandler,this);
			var cancelCmd = new qx.ui.core.Command();
			cancelCmd.addListener("execute",this.__onCancelButtonClickHandler,this);
			this.populateActionBar({
				buttons:[
							{id:"save", type:"SaveButton",label:"Create",command:saveCmd},
							{id:"close", type:"CloseButton", label: "Cancel",command:cancelCmd}
						]
			},control);
			//this.setActionBarButtonsProperties({width:80,height:20});
		},
		
		__onSaveButtonClickHandler: function(evt) {
			if (this.__form.validate()) {
				this.setStateWithDescription("running",this.tr("Creating account, please wait..."));
				this.__service.createUser(this.__model);
			} else {
				this.setStateWithDescription("error",this.tr("Please correct the error(s) on this form"));
			}
		},
		
		__onCancelButtonClickHandler: function(evt) {
			this.hide();
		},
		
		_beforeShow: function() {
			//this.setStateWithDescription("ready",this.tr("Use this form to fill a new survey"));
			
	    }
		
	    

		/*
	  	*****************************************************************************
	     UI
	  	*****************************************************************************
	 	*/
		

	},
	/*
	 * ****************************************************************************
	 * DESTRUCTOR
	 * ****************************************************************************
	 */

	destruct : function() {
		this._disposeObjects("__model","__form","__service");
	}
	
});

